﻿//审批流程
function orderapprovalprocessLoad() {
    debugger;
    ////获取当前页面的状态(注:1为新建,2为修改)
    //var formtype = Xrm.Page.ui.getFormType();
    //if (formtype == 1) {
    //    setAttributeDisabled();
    //}
    var enablestatus = Xrm.Page.getAttribute("new_enablestatus").getValue();
    if (enablestatus == 0) {
        var formtype = Xrm.Page.ui.getFormType();
        if (formtype == 2) {
            setAttributeDisabled();
            setAttributeValue();
        } else {
            setAttributeValue();
        }
    }
}

function enableButton() {
    Xrm.Utility.confirmDialog("是否确定启用？", function () {
        Xrm.Page.getAttribute("new_enablestatus").setValue(true);
        Xrm.Page.data.save();
    });
}


function deactivateButton() {
    debugger;
    var territory = Xrm.Page.getAttribute("new_territory").getValue();
    var result = GetSalesOrderInfo(territory);
    if (result != null && result.length>0) {
        Xrm.Utility.confirmDialog("该审批流程有订单在审核中,不允许停用");
    } else {
        Xrm.Utility.confirmDialog("是否确定停用？", function () {
            Xrm.Page.getAttribute("new_enablestatus").setValue(false);
            Xrm.Page.data.save();
        });
    }   
}



function setAttributeValue() {
    //节点一审批人
    Xrm.Page.getAttribute("new_handler1").addOnChange(function () {
        debugger;
        var handler1 = Xrm.Page.getAttribute("new_handler1").getValue();
        if (handler1 != null && handler1.length > 0) {
            Xrm.Page.getControl("new_handler2").setDisabled(false);
        } else {
            Xrm.Page.getControl("new_handler2").setDisabled(true);
            Xrm.Page.getControl("new_handler3").setDisabled(true);
            Xrm.Page.getControl("new_handler4").setDisabled(true);
            Xrm.Page.getControl("new_handler5").setDisabled(true);
            Xrm.Page.getAttribute("new_handler2").setValue(null);
            Xrm.Page.getAttribute("new_handler3").setValue(null);
            Xrm.Page.getAttribute("new_handler4").setValue(null);
            Xrm.Page.getAttribute("new_handler5").setValue(null);
        }
    });

    
    //节点二审批人
    Xrm.Page.getAttribute("new_handler2").addOnChange(function () {
        debugger;
        var new_handler1 = Xrm.Page.getAttribute("new_handler1").getValue();
        if (new_handler1 == null ) {
            Xrm.Page.getAttribute("new_handler2").setValue(null);
            Xrm.Utility.confirmDialog("请输入第一节点审批人");
            return;
        }
        var new_handler2 = Xrm.Page.getAttribute("new_handler2").getValue();
        if (new_handler2 != null && new_handler2.length > 0) {
            Xrm.Page.getControl("new_handler3").setDisabled(false);
            Xrm.Page.getAttribute("new_deliverytime2").setRequiredLevel("required");
            Xrm.Page.getAttribute("new_deliverytimesign2").setRequiredLevel("required");
            Xrm.Page.getAttribute("new_orderamount2").setRequiredLevel("required");
            Xrm.Page.getAttribute("new_orderamountsign2").setRequiredLevel("required");
        } else {
            Xrm.Page.getControl("new_handler3").setDisabled(true);
            Xrm.Page.getControl("new_handler4").setDisabled(true);
            Xrm.Page.getControl("new_handler5").setDisabled(true);
            Xrm.Page.getAttribute("new_handler3").setValue(null);
            Xrm.Page.getAttribute("new_handler4").setValue(null);
            Xrm.Page.getAttribute("new_handler5").setValue(null);
            Xrm.Page.getAttribute("new_deliverytime2").setRequiredLevel("none");
            Xrm.Page.getAttribute("new_deliverytimesign2").setRequiredLevel("none");
            Xrm.Page.getAttribute("new_orderamount2").setRequiredLevel("none");
            Xrm.Page.getAttribute("new_orderamountsign2").setRequiredLevel("none");
        }
    });
    //节点三审批人
    Xrm.Page.getAttribute("new_handler3").addOnChange(function () {
        debugger;
        var new_handler2 = Xrm.Page.getAttribute("new_handler2").getValue();
        if (new_handler2 == null) {
            Xrm.Page.getAttribute("new_handler3").setValue(null);
            Xrm.Utility.confirmDialog("请输入第二节点审批人");
            return;
        }
            var handler3 = Xrm.Page.getAttribute("new_handler3").getValue();
        if (handler3 != null && handler3.length > 0) {
            Xrm.Page.getControl("new_handler4").setDisabled(false);
            Xrm.Page.getAttribute("new_deliverytime3").setRequiredLevel("required");
            Xrm.Page.getAttribute("new_deliverytimesign3").setRequiredLevel("required");
            Xrm.Page.getAttribute("new_orderamount3").setRequiredLevel("required");
            Xrm.Page.getAttribute("new_orderamountsign3").setRequiredLevel("required");
        } else {
            Xrm.Page.getControl("new_handler4").setDisabled(true);
            Xrm.Page.getControl("new_handler5").setDisabled(true);
            Xrm.Page.getAttribute("new_handler4").setValue(null);
            Xrm.Page.getAttribute("new_handler5").setValue(null);
            Xrm.Page.getAttribute("new_deliverytime3").setRequiredLevel("none");
            Xrm.Page.getAttribute("new_deliverytimesign3").setRequiredLevel("none");
            Xrm.Page.getAttribute("new_orderamount3").setRequiredLevel("none");
            Xrm.Page.getAttribute("new_orderamountsign3").setRequiredLevel("none");
        }
    });
    //节点四审批人
    Xrm.Page.getAttribute("new_handler4").addOnChange(function () {
        debugger;
        var new_handler3 = Xrm.Page.getAttribute("new_handler3").getValue();
        if (new_handler3 == null) {
            Xrm.Utility.confirmDialog("请输入第三节点审批人");
            Xrm.Page.getAttribute("new_handler4").setValue(null);
            return;
        } else {
            var handler4 = Xrm.Page.getAttribute("new_handler4").getValue();
            if (handler4 != null && handler4.length > 0) {
                Xrm.Page.getControl("new_handler5").setDisabled(false);
                Xrm.Page.getAttribute("new_deliverytime4").setRequiredLevel("required");
                Xrm.Page.getAttribute("new_deliverytimesign4").setRequiredLevel("required");
                Xrm.Page.getAttribute("new_orderamount4").setRequiredLevel("required");
                Xrm.Page.getAttribute("new_orderamountsign4").setRequiredLevel("required");
            } else {
                Xrm.Page.getControl("new_handler5").setDisabled(true);
                Xrm.Page.getAttribute("new_handler5").setValue(null);
                Xrm.Page.getAttribute("new_deliverytime4").setRequiredLevel("none");
                Xrm.Page.getAttribute("new_deliverytimesign4").setRequiredLevel("none");
                Xrm.Page.getAttribute("new_orderamount4").setRequiredLevel("none");
                Xrm.Page.getAttribute("new_orderamountsign4").setRequiredLevel("none");
            }
        }
    });

    //节点五审批人
    Xrm.Page.getAttribute("new_handler5").addOnChange(function () {
        debugger;
        var new_handler4 = Xrm.Page.getAttribute("new_handler4").getValue();
        if (new_handler4 == null) {
            Xrm.Page.getAttribute("new_handler5").setValue(null);
            Xrm.Utility.confirmDialog("请输入第四节点审批人");
            return;
        }
        var handler5 = Xrm.Page.getAttribute("new_handler5").getValue();
        if (handler5 != null && handler5.length > 0) {
            Xrm.Page.getAttribute("new_deliverytime5").setRequiredLevel("required");
            Xrm.Page.getAttribute("new_deliverytimesign5").setRequiredLevel("required");
            Xrm.Page.getAttribute("new_orderamount5").setRequiredLevel("required");
            Xrm.Page.getAttribute("new_orderamountsign5").setRequiredLevel("required");
        } else {
            Xrm.Page.getAttribute("new_deliverytime5").setRequiredLevel("none");
            Xrm.Page.getAttribute("new_deliverytimesign5").setRequiredLevel("none");
            Xrm.Page.getAttribute("new_orderamount5").setRequiredLevel("none");
            Xrm.Page.getAttribute("new_orderamountsign5").setRequiredLevel("none");
        }
    });
}


function setAttributeDisabled() {
    Xrm.Page.getControl("new_handler2").setDisabled(false);
    Xrm.Page.getControl("new_handler3").setDisabled(false);
    Xrm.Page.getControl("new_handler4").setDisabled(false);
    Xrm.Page.getControl("new_handler5").setDisabled(false);
}






function GetSalesOrderInfo(quyu) {
    var fetchXml = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>" +
        "   <entity name='salesorder'>" +
        "     <attribute name='salesorderid'/>" +
        "     <order attribute='name' descending='false'/>" +
        "     <filter type='and'>" +
        "       <condition attribute='new_quyu' operator='eq' value='" + quyu + "'/>" +
        "       <condition attribute='new_status' operator='eq' value='100000006'/>" +//审核中
        "       <condition attribute='statecode' operator='eq' value='0' />" +
        "     </filter>" +
        "   </entity>" +
        " </fetch>";
    var retrTotalQuota = XrmServiceToolkit.Soap.Fetch(fetchXml);
    if (retrTotalQuota && retrTotalQuota.length > 0) {
        return retrTotalQuota;
    }
    return null;
}